-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CSS Grid utilities #1274
Add CSS Grid utilities #1274
Conversation
How about adding the |
Nice one! |
I think you should drop 'grid-' from grid-column-gap and grid-row-gap as the spec now defines these as simply row-gap and column-gap https://developer.mozilla.org/en-US/docs/Web/CSS/gap Also because these properties will apply to the subgrid and flex display types soon too |
There is no |
Ahh my bad, mistook the heading of the related sections. Looks like a really solid and flexible implemention |
@adarsh4d Good catch on the One thing I'm wondering, in the latest version of the spec these properties are just Should we just use the old names since they will be supported forever anyways? Should we manually prefix? Should I annoy @ai to reconsider prefixing them in autoprefixer? 👀 |
@adamwathan Remember the bug in Flexbox and CSS Grid with overflow and fr units. Solution: Use the hack |
@Dan503 do you think that it is safe to use Autoprefixer Grid in this case? |
@MrJmpl3 I'm not familiar, any chance you could share something for me to learn more about it? Edit: Looks like it's explained pretty well here: https://css-tricks.com/preventing-a-grid-blowout/ I'll update to use minmax(0, 1fr) instead of just 1fr 👍 |
Pushed a change to rename the |
|
You might also want to add If not, people will be flooded with the following warning message:
|
@adamwathan Here explain the bug, https://css-tricks.com/preventing-a-grid-blowout/ Edit: I see the edit. |
This comment has been minimized.
This comment has been minimized.
@Dan503 Thanks a ton for your input, really appreciate it!
My instinct is definitely to agree with you but every time I try to think of real-world practical situations where this would be useful I struggle to come up with any. To me it feels like being able to control which row an element appears on is not useful unless there are a fixed number of rows 🤔 Can you think of any good examples of layouts/designs that we can use as a reference for helping determine what default values would actually be helpful? |
It is useful for creating vertical white space in the design. Try looking through some of the crazy things Jen Simmons has done as demos.
When I said this, I was not referring to autoplacement. You can place things on the implicit grid explicitly. This code pen shows what I mean. That pen also works in IE if you view it in Debug mode. https://codepen.io/daniel-tonon/pen/YvjLaa
What I meant by that is that doing something like The one use case where this logic breaks is if you want to use |
I asked Andrey if he could add He said that he would add a warning to use |
Would it be possible to import the grid component of Tailwind into projects without importing all of Tailwind? I can see myself using these grid utilities but I don't want to go all in on using Tailwind to style everything on my sites. |
@Dan503 If you use PurgeCSS, any Tailwind classes you don't use will be gone anyway. |
Yep this is totally possible, you can use the To just get grid utilities you'd do this: // tailwind.config.js
module.exports = {
corePlugins: [
'display', // to get the `.grid` utility, but you'll also get `.block`, `.inline`, etc. so consider if you actually want that
'gridTemplateColumns',
'gridTemplateRows',
'gap',
'columnGap',
'rowGap',
'gridColumn',
'gridColumnStart',
'gridColumnEnd',
'gridRow',
'gridRowStart',
'gridRowEnd',
} |
Gaps are possible through Autoprefixer...though it needs to all be in the same rule so you might have a point there. I disagree in general about CSS Grid being more trouble than it is worth though. All I'm asking for are a few Autoprefixer control comments to prevent the warnings from appearing, then developers can decide for themselves if they think it is worth the trouble or not. |
If I'm not wrong the grid implementation in tailwind is aiming to bring basic columns and rows. If there's no autoplacment in IE and you need a class for every cell in order to place it in the right slot, that means more code than just using flexbox. I get it, you only need a comment in there. But maybe other devs don't want such a verbose output. I could be wrong. |
These warnings will only appear with the I still think you should just not use CSS Grid if you have to support legacy browsers personally. I've literally never actually needed CSS Grid for any project I've ever worked on 👀 Somewhat related, still surprised prefixing |
I love grid, you can build columns in a breeze with much less code. You can even (almost) build responsive columns without mediaqueries. The problem is grid-template-columns —when using When /* Now */
.mygrid {
display: grid;
gap: 1.5rem;
/* Horizontal overflow in small devices */
grid-template-column: repeat(auto-fit, minmax(400px, 1fr));
}
/* When min, max and clamp get wider support */
.mygrid {
display: grid;
gap: 1.5rem;
/* No more overflow issues */
grid-template-columns: repeat(auto-fit, minmax(min(400px, 100%), 1fr));
} |
@adamwathan There are some cases when using Grid makes more sense than Flexbox. Supporting IE11 in those cases is less painful. One of those scenarios could be when building masthead menus (logo on one side, menu in the other). Wes Boss has a great free course about CSS Grid https://cssgrid.io/. In "Flexbox Vs CSS Grid" video he explains good cases for using CSS Grid over Flexbox. Worth checking out. |
I put together a bit of a demo to demonstrate how people might make use of rows in tailwind: https://codepen.io/daniel-tonon/pen/YzPEOWN It is IE11 compatible. You can view the IE version by viewing in Debug mode and pasting the debug mode url into IE11. I didn't make it responsive since the goal was just to show the use case. The row declarations for the main outer layout aren't necessary in modern browsers but they are needed for IE compatibility. The rows for the listing are necessary in modern browsers since the image comes after the title in the HTML for accessibility. |
How about adding negative start\end columns by default? module.exports = {
theme: {
numbers: {
'1': '1',
'2': '2',
'3': '3',
// ...
},
extend: {
gridColumnEnd: (theme, { negative }) => negative(theme('numbers')),
gridColumnStart: (theme, { negative }) => negative(theme('numbers')),
},
},
} imho it's really useful and rarely mentioned.
|
Just to chime in on grid rows and why I believe that the gridRow, gridRowStart and gridRowEnd should be included in the default. So I am currently building the following design. This is to a degree a standard desktop layout of a modern design, where elements sit in columns that overlap rows and sometimes will overlap columns. Looking at several of my recent designs and other designs across the web, non-symmetrical layouts and overlapping design elements are quite trendy and becoming quite common (IMO and from my observation). So taking the above design, I started to craft this together using the current 1.2.0-canary.5 build (at the time of writing). It become apparent quite early on that I was going to need css-grid rows.
What this allowed me to do was to set the rows for the main areas:
By doing this I was then able to get the breadcrumbs and content sitting next to each other without a huge gap as without setting any rows, the rows would automatically be the height of the tile. As this was the first row. This screenshot illustrates best: By adding the rows into the config, I could then create the following: Here is the final html that was used for the above:
I think when it comes to building layouts, including the CSS grid rows is essential. It gives much more flexibility out the box, to be able to position elements and also finer control to move the order of these items around across different devices/breakpoints. I think without rows, you are pretty limited to building just simply grids that are equal. So things like card and tile (grid) layouts, like this: Based on the above @adamwathan, if you agree to implement then it also might be worth changing the naming for the gridColumn, gridColumnStart and gridColumnEnd if they were used across both columns and rows, as I did in my config. |
@adamwathan would you also consider including https://www.deeson.co.uk/blog/why-i-often-use-24-column-grid |
I feel like supporting 24 columns by default adds a bit too much bloat 😕 |
In my opinion there are not a lot of people who use it on regular basis. You can always extended the column to your needs, or create a plugin and use it in all the projects. |
did i miss a note on justify\align\content ?
|
@gcoda Seems this might be the case... @adamwathan said in #868 (comment)
|
@adamwathan did you have any further thoughts on rows and also negative grid lines? |
getting flooded with dozens and dozens of warnings
anyone know how I can stop those? |
@vesper8 I think this is coming from Autoprefixer. |
@terryupton ahh.. I see.. the only mention of autoprefixer anywhere is here in my postcss.config.js
I had a freelancer designer add this there.. unsure if it's necessary.. I should set grid to false you think? |
@vesper8 I think setting it to false will remove those warnings, whether that is a bug I am not sure? |
@vesper8 yes, you will need to set grid to false. If you are writing any custom CSS and you want to support IE11, you can add a control comment inside the rule like this: .custom-grid-container {
/* autoprefixer grid: autoplace */
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(5, auto);
} The tailwind grid implementation doesn't support IE11. It could support it if it was able to output control comments to disable certain rules but apparently the infrastructure to support that hasn't been built. |
There is a way to disable all this? Im using corePlugins: {
gap: false,
columnGap: false,
rowGap: false,
gridColumn: false,
gridColumnEnd: false,
gridColumnStart: false,
gridRow: false,
gridRowEnd: false,
gridRowStart: false,
gridTemplateColumns: false,
gridTemplateRows: false,
}, But there is still grid classes in resulting .css |
Never mind. |
That sounds like a lot of work to disable CSS Grid 😕 If you replace all of the grid ones with just If it doesn't already then I think the functionality should be added. |
No, it does not. 😢
👍 |
About Here's an example UI: I was able to achieve it by adding a Maybe there's another way to achieve that, dunno |
This PR adds support for CSS Grid Layout to Tailwind CSS.
Here is an example of what it looks like to use these new utilities:
It adds a new
grid
value to thedisplay
core plugin, as well as provides new core plugins for the following CSS properties:grid-template-columns
grid-column-gap
grid-column
grid-column-start
grid-column-end
grid-template-rows
grid-row-gap
grid-row
grid-row-start
grid-row-end
grid-template-columns
These utilities take the form
grid-cols-{key}
where "key" is any value configured under thegridTemplateColumns
key in your theme config.By default we provide the necessary values to create basic equal width column grids with up to 12 columns:
I've opted to avoid any weird magic here and instead just given the user complete control over the value of this property, so although I'm just using
repeat(x, 1fr)
here for each value, there's absolutely nothing stopping you from creating something more custom like so:Providing a basic 12 column grid seemed like the most sensible thing to do by default, as there aren't many other good ideas I can think of that are actually general purpose and not hyper-specific to one site design.
grid-column-gap
These utilities take the form
col-gap-{key}
where "key" is any value configured under thegridColumnGap
key in your theme config.By default, this uses your
spacing
config, and therefore matches all of your padding/margin/width/height utilities.grid-column
These utilities take the form
col-{key}
where "key" is any value configured under thegridColumn
key in your theme config.By default we only provide values for spanning across columns:
Again no magic or assumptions here at all — I've included the word
span
explicitly in each key to make sure the utilities are generated likecol-span-5
for our default values, but you can totally do whatever you want here, like:...to generate a class that fills a named column area like
col-logo-area
.The reason I've duplicated the value (like
span 2 / span 2
) is so that you can easily override just thegrid-column-start
orgrid-column-end
value without losing thespan
value, like this:...which would create an element that spans 4 columns but starts at grid line 2.
grid-column-start
These utilities take the form
col-start-{key}
where "key" is any value configured under thegridColumnStart
key in your theme config.By default we only provide values that match the grid lines created by our default
gridTemplateColumns
config:grid-column-end
These utilities take the form
col-end-{key}
where "key" is any value configured under thegridColumnEnd
key in your theme config.By default we only provide values that match the grid lines created by our default
gridTemplateColumns
config:grid-template-rows
These utilities take the form
grid-rows-{key}
where "key" is any value configured under thegridTemplateRows
key in your theme config.By default we do not provide any values for this utility at all:
That means no
grid-rows-{key}
utilities are generated by default, and if you'd like to add them you need to customize your config.This is because I just could not think of any sensible general purpose default values for this — any real-world use case I could come up with seemed too specific to a given design.
grid-row-gap
These utilities take the form
row-gap-{key}
where "key" is any value configured under thegridRowGap
key in your theme config.By default, this uses your
spacing
config, and therefore matches all of your padding/margin/width/height utilities.grid-row
These utilities take the form
row-{key}
where "key" is any value configured under thegridRow
key in your theme config.By default we do not provide any values for this utility at all:
That means no
row-{key}
utilities are generated by default, and if you'd like to add them you need to customize your config.This is for the same reason as
grid-template-rows
.grid-row-start
These utilities take the form
row-start-{key}
where "key" is any value configured under thegridRowStart
key in your theme config.By default we do not provide any values for this utility at all:
That means no
row-start-{key}
utilities are generated by default, and if you'd like to add them you need to customize your config.This is for the same reason as
grid-template-rows
.grid-row-end
These utilities take the form
row-end-{key}
where "key" is any value configured under thegridRowEnd
key in your theme config.By default we do not provide any values for this utility at all:
That means no
row-end-{key}
utilities are generated by default, and if you'd like to add them you need to customize your config.This is for the same reason as
grid-template-rows
.What's not included
grid-auto-columns
utilitiesgrid-auto-rows
utilitiesgrid-auto-flow
utilitiesgrid-template-areas
utilitiesIf you think any of these are super important in a utility context, please reply with a good example and any ideas you have on how it should be included and we can definitely talk about getting it in.
Try it out
Here's a simple tailwind.run where I've thrown in the pre-compiled CSS so you can play with it:
https://tailwind.run/MSuVJB/1
Any feedback appreciated!